/* 1. 引入 axios */
import axios from "axios";
const instance = axios.create({
  baseURL: "http://s.linweiqin.com/",
});
// 添加拦截器
// 请求和响应的拦截器
// Add a request interceptor
instance.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    // DONE 将 auth_token 将它加到请求数据中
    let wdata = JSON.parse(localStorage.getItem("user_id") || "{}");
    // console.log(wdata);
    let oauth_token = wdata;
    if (config.method === "get") {
      if (oauth_token) {
        config.url = config.url + "?oauth_token=" + oauth_token;
      }
    } else {
      config.data = config.data || {};
      if (oauth_token) {
        config.data["oauth_token"] = oauth_token;
      }
    }
    // console.log(config);
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
instance.interceptors.response.use(
  function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response.data;
  },
  function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  }
);

export default instance;
